﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using bmapi.SQLServerAccess;
using static bmapi.Models.ShowModel;

namespace bmapi.Controllers
{
    [Route("staffmanage")]
    public class StaffManageController : PageController
    {

        public HttpResponseMessage GetStaffs(int page = 1, int limit = 10)
        {
            int loginid = GetCrrentIDByToken();
            if (loginid == 0)
                return AssistFun.toJson(new {status=201,tip="不存在工号:0" });
            User user = GetUserByLoginID(loginid);
            int totalPage = 0;
            int totalRows = 0;

            DataTable dtList = SQLCommand.GetDataListByPage("User_tb", "*", "login_id", " and PLID="+user.PLID, limit, page, out totalPage, out totalRows);
            if (dtList != null)
            {
                List<User> users = AssistFun.DataTableToList<User>(dtList);
                //获取工序名称 
                List<int> list = dtList.AsEnumerable().Select(t => t.Field<int>("code")).ToList();
                List<string> listCodeName = MatchProNameByProCode(list);
                List<ShowStaffInfo> pageshow = new List<ShowStaffInfo>();
                for (int i = 0; i < users.Count; i++)
                {
                    pageshow.Add(new ShowStaffInfo(users[i], listCodeName[i]));
                }
                return AssistFun.toJson(new RespondModel(200, "成功", new data(totalRows, pageshow.ConvertAll(u => (object)u))));
            }
            else
            {
                return AssistFun.toJson(new RespondModel(201, "没有数据"));
            }
        }
        /// <summary>
        /// 修改员工密码 user实体做参数     
        /// </summary> 
        /// <param name="id"></param>
        [HttpPut]
        public HttpResponseMessage Put(dynamic obj)
        {
            string sql = "update User_tb set code=@code,password=@pwd,mach_id=@mid,state=@state where login_id=@loginid";
            SQLCommand sc = new SQLCommand(sql);
            sc.Add("@code", Convert.ToInt32(obj.code.ToString()));
            sc.Add("@pwd", obj.password.ToString());
            sc.Add("@mid", Convert.ToInt32(obj.mach_id));
            sc.Add("@state", Convert.ToInt32(obj.state));
            sc.Add("@loginid", Convert.ToInt32(obj.login_id));
            
            if (sc.ExecuteNonQuery() == -1)
                return AssistFun.toJson(new RespondModel(201, "修改失败"));
            else
                return AssistFun.toJson(new RespondModel(200, "成功"));
        }
        /// <summary>
        /// 新增员工  只有两个信息 工号login_id  工序 code 默认密码888888
        /// </summary>    
        /// <param name="obj"></param>
        [HttpPost]
        public HttpResponseMessage Post(dynamic obj)
        {
            int currentLid = GetCrrentIDByToken();
            if (currentLid == 0)
                return AssistFun.toJson(new {status=201,tip="不存在工号:0" });
            User user = GetUserByLoginID(currentLid);
            int loginid = Convert.ToInt32(obj.login_id);

            SQLCommand sc = null;
            sc = new SQLCommand("select login_id from User_tb where login_id=" + loginid);
            string id = sc.ExecuteScalar();

            if(id!=null)
                return AssistFun.toJson(new RespondModel(201, "新增失败,已存在工号"+loginid));

            sc = new SQLCommand("insert into User_tb(login_id,code,PLID) values(@loginid,@code,@plid)");
            sc.Add("@loginid", loginid);
            sc.Add("@code", Convert.ToInt32(obj.code));
            sc.Add("@plid", user.PLID);
            
            if (sc.ExecuteNonQuery() == 1)
                return AssistFun.toJson(new RespondModel(200, "成功"));
            else
                return AssistFun.toJson(new RespondModel(201, "新增失败"));
        }


    }
}
